<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Goods;
use plugin\admin\app\model\GoodsCommentReply;
use plugin\admin\app\model\UserProfile;
use support\Request;
use support\Response;
use plugin\admin\app\model\GoodsComment;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 商品评价 
 */
class GoodsCommentController extends Crud
{
    
    /**
     * @var GoodsComment
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new GoodsComment;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('goods-comment/index');
    }

    public function reply(Request $request): Response
    {
        if ($request->isAjax()) {
            GoodsCommentReply::insert([
                'comment_id' => $request->post('comment_id'),
                'goods_id' => $request->post('goods_id'),
                'desc' => $request->post('desc'),
                'created_time' => date('Y-m-d H:i:s')
            ]);
            return $this->json(0, '成功');
        }

        return view('goods-comment/reply', [
            'id' => $request->get('id'),
            'goods_id' => $request->get('goods_id')
        ]);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('goods-comment/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('goods-comment/update');
    }

    protected function formatNormal($items, $total): Response
    {
        $goodsIds = array_column($items, 'goods_id');
        $goods = Goods::whereIn('id', $goodsIds)->get()->toArray();
        $goods = array_combine(
            array_column($goods, 'id'),
            array_values($goods)
        );

        $userIds = array_column($items, 'user_id');
        $users = UserProfile::whereIn('user_id', $userIds)->get()->toArray();
        $users = array_combine(
            array_column($users, 'user_id'),
            array_values($users)
        );

        $commentIds = array_column($items, 'id');
        $reply = GoodsCommentReply::whereIn('comment_id', $commentIds)->get()->toArray();
        $reply = array_combine(
            array_column($reply, 'comment_id'),
            array_values($reply)
        );

        foreach ($items as $key=>$item) {
            $item['goods_info'] = $goods[$item['goods_id']];
            $item['user_info'] = $users[$item['user_id']];
            $item['reply_info'] = $reply[$item['id']] ?? null;
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
}
